﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Linq;
namespace BudgetManager.Domain
{
    public class Account
    {


        List<AbstractValue> incomes = new List<AbstractValue>();
        List<AbstractValue> expenses = new List<AbstractValue>();
        List<Goal> goals = new List<Goal>();

        public Account(int id, string name, string password, string description)
        {
            Id = id;
            Name = name;
            Pin = password;
            Description = description;

            incomes.Add(new Income { Category = Category.readIncomeCategoryByName("Salário"), Money = 600, FullDate = DateTime.Now, Id = 1 ,Description ="EDP"} );
            expenses.Add(new Outcome { Category = Category.readOutcomeCategoryByName("Vestuário"), Money = 30, FullDate = DateTime.Now, Id = 2,Description ="Zara" });


        }
        public Account(int id, string name, string password, string description, Goal goal)
            : this(id, name, password, description)
        {
            goals.Add(goal);

        }
        public void addGoal(Goal goal)
        {
            goals.Add(goal);
        }
        public void addIncome(Income income)
        {
            int lastId = incomes.Count;
            income.Id = lastId + 1;
            incomes.Add(income);
        }
        public void addExpense(Outcome expense)
        {
            int lastId = incomes.Count;
            expense.Id = lastId + 1;
            expenses.Add(expense);
        }

        //Obtem a lista despesas e receitas
        public List<AbstractValue> GetTopActivities()
        {
            List<AbstractValue> activities = new List<AbstractValue>();
            activities.AddRange(incomes);
            activities.AddRange(expenses);
            return activities.OrderByDescending(e => e.FullDate).ToList();
        }
        public double GetTotalExpenses()
        {
            double sumExpenses = 0;
            foreach (AbstractValue expense in expenses)
            {
                sumExpenses += expense.Money;
            }

            return sumExpenses;
        }

        public double GetTotalIncomes()
        {
            double sumIncomes = 0;
            foreach (AbstractValue income in incomes)
            {
                sumIncomes += income.Money;
            }
            return sumIncomes;
        }

        public double GetCurrentBalance()
        {
            return GetTotalIncomes() - GetTotalExpenses();
        }

        public double GetMonthIncomes()
        {
            double sumIncomes = 0;
            DateTime now = DateTime.Now;
            foreach (AbstractValue income in incomes)
            {
                if (income.FullDate.Year == now.Year && income.FullDate.Month == now.Month)
                {
                    sumIncomes += income.Money;
                }
            }
            return sumIncomes;
        }
        public double GetMonthExpenses()
        {
            double sumExpenses = 0;
            DateTime now = DateTime.Now;
            foreach (AbstractValue expense in expenses)
            {
                if (expense.FullDate.Year == now.Year && expense.FullDate.Month == now.Month)
                {
                    sumExpenses += expense.Money;
                }
            }
            return sumExpenses;
        }
        public int Id { get; private set; }
        public String Name { get; set; }
        public String Description { get; set; }
        public String Pin { get; set; }

        public List<Goal> GetGoals()
        {
            return goals.ToList();
        }

        public void editExpense(int id, Category _category, string descritpion, int money)
        {
            for (int i = 0; i < expenses.Count; i++)
            {
                if (expenses[i].Id == id)
                {
                    expenses[i].Category = _category;
                    expenses[i].Description = descritpion;
                    expenses[i].Money = money;
                    break;
                }
            }
        }

        public void editIncome(int id, Category _category, string descritpion, int money)
        {
            for (int i = 0; i < incomes.Count; i++)
            {
                if (incomes[i].Id == id)
                {
                    incomes[i].Category = _category;
                    incomes[i].Description = descritpion;
                    incomes[i].Money = money;
                    break;
                }
            }
        }

        internal System.Collections.IEnumerable GetExpensesOrderByDate()
        {
            return expenses.OrderByDescending(e => e.FullDate).ToList();
        }

        internal System.Collections.IEnumerable GetIncomesOrderByDate()
        {
            return incomes.OrderByDescending(e => e.FullDate).ToList();
        }

        internal void deleteExpense(int id)
        {
            for (int i = 0; i < expenses.Count; i++)
            {
                if (expenses[i].Id == id)
                {
                    expenses.RemoveAt(i);
                    return;
                        
                }
            }
        }
        internal void deleteIncome(int id)
        {
            for (int i = 0; i < incomes.Count; i++)
            {
                if (incomes[i].Id == id)
                {
                    incomes.RemoveAt(i);
                    return;

                }
            }
        }

        public void deleteGoal(Goal goal)
        {
            goals.Remove(goal);
        }

        internal void deleteGoal(object p)
        {
            goals.Remove(((Goal)p));
        }
    }
}

